package com.panda.admin.interfaces.param.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panda.admin.base.BaseEnum;
import com.panda.admin.interfaces.param.service.SystemParamService;
import com.panda.admin.interfaces.param.mapper.SystemParamMapper;
import com.panda.admin.login.dto.ParamDto;
import com.panda.admin.login.entity.SystemParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* @author tucy123
* @description 针对表【system_param(系统参数表)】的数据库操作Service实现
* @createDate 2023-07-10 15:26:06
*/
@Service
public class SystemParamServiceImpl extends ServiceImpl<SystemParamMapper, SystemParam> implements SystemParamService{

    @Autowired
    SystemParamMapper systemParamMapper;

    /**
     * 条件分页查询
     * @param paramDto
     * @return
     */
    @Override
    public Page<SystemParam> pageList(ParamDto paramDto) {
        Page<SystemParam> page = new Page<>(paramDto.getPageNum(),paramDto.getPageSize());

        QueryWrapper<SystemParam> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(ObjectUtil.isNotEmpty(paramDto.getType()),SystemParam::getTypeName,paramDto.getType()).or()
                .like(ObjectUtil.isNotEmpty(paramDto.getType()),SystemParam::getTypeCode,paramDto.getType());
        wrapper.lambda()
                .like(ObjectUtil.isNotEmpty(paramDto.getParam()),SystemParam::getParamName,paramDto.getParam()).or()
                .like(ObjectUtil.isNotEmpty(paramDto.getParam()),SystemParam::getParamCode,paramDto.getParam());
        wrapper.orderByDesc("sort");

        return systemParamMapper.selectPage(page,wrapper);
    }

    /**
     * 查询参数详情
     * @param id
     * @return
     */
    @Override
    public SystemParam info(Long id) {
        return systemParamMapper.selectById(id);
    }

    /**
     * 增改参数
     * @param systemParam
     * @return
     */
    @Override
    public Boolean saveOrUpdateParam(SystemParam systemParam) {
        List<SystemParam> paramList = this.lambdaQuery().eq(SystemParam::getTypeCode, systemParam.getTypeCode()).list();

        if (ObjectUtil.isNotEmpty(paramList)){
            systemParam.setSort(paramList.get(0).getSort());
        } else {
            Integer count = systemParamMapper.paramCount();
            systemParam.setSort(count);
        }

        return this.saveOrUpdate(systemParam);
    }

    /**
     * 删除参数
     * @param ids
     * @return
     */
    @Override
    public Boolean deleteParams(List<Long> ids) {
        int i = systemParamMapper.deleteBatchIds(ids);
        return i != 1;
    }

    /**
     * 查询参数类型
     * @return
     */
    @Override
    public List<SystemParam> queryTypes() {
        List<SystemParam> paramList = this.list();

        return paramList.stream().filter(distinctByKey(SystemParam::getTypeCode)).collect(Collectors.toList());
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }
}




